#include "radiometerform.h"
#include "ui_radiometerform.h"
#include "../deviceClass/radiometer.h"

RadiometerForm::RadiometerForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RadiometerForm)
{
    ui->setupUi(this);

    this->InitTreeView();
}

RadiometerForm::~RadiometerForm()
{
    delete ui;
}


void RadiometerForm::InitTreeView()
{
    ui->tableView_2->setWindowTitle("数据列表");
    ui->tableView_2->setWindowIconText("数据列表");
    m_model.setRowCount(0);
    m_model.setColumnCount(12);
    m_model.setHorizontalHeaderLabels(QStringList() << "水汽"
                                                    << "时间"
                                                    <<"波长1020"<<"波长1640I"<<"波长870"
                                                    <<"波长675"<<"波长440"<<"波长500"
                                                    <<"波长1020I"<<"波长937"<<"波长380"<<"波长340");

    QHeaderView *headerView = ui->tableView_2->horizontalHeader();
    // 设置样式表，包括背景颜色和字体样式
    headerView->setSectionResizeMode(QHeaderView::ResizeMode::Stretch);
    headerView->setStyleSheet("QHeaderView::section { background-color: #8da1ff; color: white }");
    headerView->setTextElideMode(Qt::TextElideMode::ElideMiddle);

    ui->tableView_2->setModel(&m_model);

    connect(RadioMeter::getInstance(), &RadioMeter::updateView_AOD, this, [=](const RadioMeterAODProtocolAll& point){
        int row = m_model.rowCount();
        if(row == 10){
            m_model.removeRows(0, 1);
            row--;
        }

        ui->label_PWV->setText(QString::asprintf("%f",point.PWV));

        auto item = new QStandardItem(QString::asprintf("%f",point.PWV));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 0, item);

        item = new QStandardItem(point.dateTime.toString("HH:mm"));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 1, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun1020));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 2, item);

        item = new QStandardItem(QString::asprintf("%f",point.sunI1640));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 3, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun870));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 4, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun675));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 5, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun440));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 6, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun500));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 7, item);

        item = new QStandardItem(QString::asprintf("%f",point.sunI1020));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 8, item);

        item = new QStandardItem(QString::asprintf("%f",point.sun937));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 9, item);

        item = new QStandardItem(QString::asprintf("%d",point.sun380));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 10, item);

        item = new QStandardItem(QString::asprintf("%d",point.sun340));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 11, item);



        //ui->tableView->update();
    });
    /*
    connect(this, &VisibilityMeterForm::updateView, this, [&](VisibilityProtocol point){
        int row = m_model.rowCount();
        if(row == 10){
            m_model.removeRows(0, 1);
            row--;
        }

        auto item = new QStandardItem(QString::asprintf("%d",point.one_minute_visibility));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 0, item);

        item = new QStandardItem(QString::asprintf("%d",point.ten_minute_visibility));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 1, item);

        item = new QStandardItem(QString::asprintf("%d",point.TheIntensityOfEmittedEnergy));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 2, item);

        item = new QStandardItem(QString::asprintf("%d",point.TheIntensityOfReceivedEnergy));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 3, item);

        item = new QStandardItem(point.device_code);
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 4, item);

        item = new QStandardItem(point.date_time.toString("yyyy/MM/dd HH:mm:ss"));
        item->setTextAlignment(Qt::AlignCenter);
        m_model.setItem(row, 5, item);

        ui->tableView->update();
    });
*/
}
